<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 * Building controller
 */
class Dspviewdata extends REST_Controller
{

    
    function __construct()
    {
        // Construct the parent class
        parent::__construct();
    }

    /**
     * [receive_get DSP数据存储]
     * @author [author]
     * @DateTime 2017-03-01T13:54:20+0800
     * @return   [type]                   [description]
     */
    public function receive_get()
    {

        $flag = false;
        $get = $this->get();
        $this->validGet($get);
        $this->load->model('buildings/dspviewlog_model');
        $dataField = ['ip_address', 'cookie', 'request_id', 'broswer_type', 'platform_type', 'adserver_id', 'os_type', 'source_url'];
        $infoData = [];
        foreach ($dataField as $val) {
            if (isset($get[$val])) {
                $infoData[$val] = $get[$val];
            }
        }
        $result = $this->dspviewlog_model->insert($infoData);
        if($result){
            $flag = true;
        }
        
        $flag ? $this->response(['status'=>200, 'data'=>$get, 'message'=>'数据写入成功'], REST_Controller::HTTP_CREATED) : $this->response(['status'=>400, 'message'=>'数据写入失败'], REST_Controller::HTTP_INTERNAL_SERVER_ERROR);
    }
    /**
     * [validPost 非空字段验证]
     * @author [author]
     * @DateTime 2017-03-01T13:54:04+0800
     * @param    [type]                   $data [description]
     * @return   [type]                         [description]
     */
    private function validGet($data)
    {
        $require = ['ip_address'=>'访问IP', 'cookie'=>'cookie值'];
        foreach ($require as $key => $val) {
            if (!isset($data[$key]) || empty($data[$key])) {
                $this->response(['status'=>401, 'message'=>$val.' 不能为空'], REST_Controller::HTTP_UNPROCESSABLE_ENTITY);
                break;
            }
        }
    }

}